//
//  ClubDetailViewCell.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/7/7.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ClubDetailViewCell.h"
#import <UIImageView+WebCache.h>
#import "YYText.h"
#import "YYImage.h"
#import "NSAttributedString+YYText.h"

#define TextFont 16
#define LineSpace 8
#define KernSpace 3
@interface ClubDetailViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *desImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageConsH;
@property (weak, nonatomic) IBOutlet YYLabel *yyLab;

@end

@implementation ClubDetailViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    //设置多行
    self.yyLab.numberOfLines = 0;
    self.yyLab.preferredMaxLayoutWidth = ScreenW-40;
}
- (CGFloat)calculateOldCellHWithModel:(OldImageDetail *)model
{
    UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:model.dynamicImage];
    CGFloat imageH = [self getImageH:image];
    CGFloat textH = [self getTextH:model.dynamicImageContent];
    return imageH+textH+30;
}
- (void)setOldModel:(OldImageDetail *)oldModel
{
    _oldModel = oldModel;
    if (!oldModel) return;
    [self.desImageView sd_setImageWithURL:[NSURL URLWithString:oldModel.dynamicImage] placeholderImage:GetImage(@"default_goods_image")];
    UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:oldModel.dynamicImage];
    if (image) {
        self.imageConsH.constant = (ScreenW-40)*image.size.height/image.size.width*1.0;
    }else {
        self.imageConsH.constant = 200;
    }
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldModel.dynamicImageContent];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_alignment = NSTextAlignmentJustified;
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    self.yyLab.textLayout = layout;
    self.yyLab.attributedText = attri;
}

- (CGFloat)calculateDetailCellHWithModel:(ImageTitleModel *)model
{
    UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:model.clubImage];
    CGFloat imageH = [self getImageH:image];
    CGFloat textH = [self getTextH:model.clubContent];
    return imageH+textH+30;
}
- (void)setModel:(ImageTitleModel *)model
{
    _model = model;
    [self.desImageView sd_setImageWithURL:[NSURL URLWithString:model.clubImage] placeholderImage:GetImage(@"default_goods_image")];
    UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:model.clubImage];
    if (image) {
        self.imageConsH.constant = (ScreenW-40)*image.size.height/image.size.width*1.0;
    }else {
        self.imageConsH.constant = 200;
    }
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:model.clubContent];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_alignment = NSTextAlignmentJustified;
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    self.yyLab.textLayout = layout;
    self.yyLab.attributedText = attri;
}

- (CGFloat)getTextH:(NSString *)str
{
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:str];
    attri.yy_font = [UIFont systemFontOfSize:TextFont];
    attri.yy_color = [UIColor blackColor];
    attri.yy_lineSpacing = LineSpace;
    attri.yy_kern = @(KernSpace);
    
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width-40, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attri];
    return layout.textBoundingSize.height;
}
- (CGFloat)getImageH:(UIImage *)image
{
    if (image) {
        CGFloat imageH = (ScreenW-40)*image.size.height/image.size.width*1.0;
        return imageH;
    }
    return 200;
}

@end
